/*
 * Copyright 2008-2012 WebEqua
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.mathcloud.math.io.scilab;

import java.io.File;

import org.mathcloud.math.core.common.quantity.MatrixObject;
import org.mathcloud.math.core.common.quantity.RealMatrix;
import org.mathcloud.math.core.scilab.factory.ScilabQuantityObjectFactory;
import org.mathcloud.math.core.scilab.manager.ScilabSupervisor;
import org.mathcloud.math.core.scilab.quantity.ScilabQuantityObject;
import org.mathcloud.math.io.string.generic.GenericOutputter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ScilabQuantityObjectOutputter extends GenericOutputter
{
	public String outputMatrix(MatrixObject arg0)
	{		
		GenericOutputter gout = new GenericOutputter();
		return "["+gout.outputMatrix(arg0, ";", ",")+"]";
	}
	
	// should be deleted - "write" function of scilab uses fortran output format 
	public File outputRealMatrix(String fileName, RealMatrix arg0)
	{
    String sciJob;
		ScilabQuantityObject tOp = ScilabQuantityObjectFactory.getInstance(arg0);    
		sciJob = "write('"+fileName+"',"+tOp.getVarName()+");";
    if (ScilabSupervisor.run(sciJob, log))
    {
    	File res = new File(fileName);
    	if (res.exists())
    		return res;
    	else return null;
    } else return null;		
	}

	private static final Logger log = LoggerFactory.getLogger(ScilabQuantityObjectOutputter.class);
}
